<?php
class ccdsObjectConfig
{
    private $serverdir;
    private $classes;
    private $extensions;
    private $ccdSQL;

    function configure( $serverdir )
    {
        if ( $serverdir != "" )
        {
            $this->serverdir=$serverdir;
            $this->classes=array();
        }
        else
        {
            return false;
        }
        $this->ccdSQL=ccdSQL::getInstance();
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */
    private function __loadClass( $class_id )
    {

        $query="SELECT * FROM classes 
							LEFT JOIN extensions ON class_key_ext_id=ext_id 
							LEFT JOIN projects ON ext_key_project_id=project_id
							WHERE class_id='" . $class_id . "'
							LIMIT 1
		";
        $res=$this->ccdSQL->query( $query );
        $class=$this->ccdSQL->fetch_assoc( $res );

        $path_array=array();
        $config_source=
            $this->serverdir . $class['project_dir'] . "/" . $class['ext_dir'] . "/" . $class['class_dir'] . "/config/"
            . $class['class_name'] . ".conf";
        if (!is_file($config_source)) return false;
        try {
        	$xml= @simplexml_load_file( $config_source, null, LIBXML_NOCDATA );
	        $this->classes[$class['class_id']]=$xml; //->getArray();
        } catch (Exception $e) {
        	$this->classes[$class['class_id']]=false;
        }
				//print_r($xml);
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

    function getClass( $class_id )
    {
        if ( !is_numeric( $class_id ) )
        {
            trigger_error( "ccdsObjectConfig: \$class_id is not an int" );
            return false;
        }

        if ( !isset( $this->classes[$class_id] ) )
        {
            $this->__loadClass( $class_id );
        }
        return $this->classes[$class_id];
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

    function saveClass( $class_id, $config )
    {
        $query="SELECT * FROM classes 
							LEFT JOIN extensions ON class_key_ext_id=ext_id 
							LEFT JOIN projects ON ext_key_project_id=project_id
							
							WHERE class_id='" . $class_id . "'
							LIMIT 1
				";
        $res=$this->ccdSQL->query( $query );
        $class=$this->ccdSQL->fetch_assoc( $res );
 

        $path_array=array();
        $config_source=
            $this->serverdir . $class['project_dir'] . "/" . $class['ext_dir'] . "/" . $class['class_dir'] . "/config/"
            . $class['class_name'] . ".conf";


				$doc = new DOMDocument('1.0');
				$doc->preserveWhiteSpace = false;
				$doc->loadXML($config->asXML());
				$doc->formatOutput = true;
				$xmlcode = $doc->saveXML();
 
				file_put_contents( $config_source, $xmlcode );
				
        $config=ccdsConfig::getInstance();
        chmod( $config_source, $config->get( 'FILE_CHMOD' ) );

        unset( $this->classes[$class['class_id']] );
        unset( $xml );
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

    private function __loadExtension( $ext_id )
    {

        $query="SELECT extensions ON class_key_ext_id=ext_id 
							LEFT JOIN projects ON ext_key_project_id=project_id
							WHERE class_id='" . $ext_id . "'
							LIMIT 1
		";
        $res=$this->ccdSQL->query( $query );
        $ext=$this->ccdSQL->fetch_assoc( $res );

        $path_array=array();
        $config_source=
            $this->serverdir . $ext['project_dir'] . "/" . $ext['ext_dir'] . "/" . $ext['class_name'] . ".conf";
        $xml = simplexml_load_file ($config_source);

        $this->extensions[$ext['ext_id']]=$xml; 
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

    function getExtension( $ext_id )
    {
        if ( !is_numeric( $ext_id ) )
            trigger_error( "ccdsObjectConfig: \$class_id is not an int" );

        if ( !is_array( $this->extensions[$ext_id] ) )
        {
            $this->__loadExtension( $ext_id );
        }
        return $this->extensions[$ext_id];
    }
/* ------------------------------------------------------------------------------------------------------------------------------ */

    function __triggerError( $error_msg )
    {
        $this->errors[]=$error_msg;
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

    function error( )
    {
        if ( count( $this->errors ) > 0 )
        {
            $errormsg=implode( "<br><br>", $this->errors );
            $this->errors=array();
            return $errormsg;
        }
        else
        {
            return false;
        }
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

	private function parse2array($columns) {
		$ret = array();
		
		foreach ($columns as $fieldname => $fieldConfig) {
			
	    if ( "row" == $fieldname || "column" == $fieldname  || is_numeric($fieldname)) {
	      $ret=array_merge( $ret, $this->parse2array( $fieldConfig) );
	    }
	    elseif('@attributes' == $fieldname) {
	    
	    } else {
	    	
	    	$ret[$fieldname] = $fieldConfig;
	    }
		}
		
		return $ret;
	}


/* ------------------------------------------------------------------------------------------------------------------------------ */

		function getFlatDataArray($class_id) {
			
			static $tmp_arrays = array();
			
			if (isset($tmp_arrays[$class_id])) return $tmp_arrays[$class_id];
			
			if ($config = $this->getClass($class_id)) {
				$columns = $config->xpath('data/columns');
				$columns = $columns[0];
				$foo = new ccdsXMLparser($columns);
				$columns = $foo->getArray();
				$tmp_arrays[$class_id] = $this->parse2array($columns);
				return $tmp_arrays[$class_id];
			} else {
				return array();
			}
		}

/* ------------------------------------------------------------------------------------------------------------------------------ */

		function getFlatParameterArray($class_id) {
			
			static $tmp_param_arrays = array();
			
			if (isset($tmp_param_arrays[$class_id])) return $tmp_param_arrays[$class_id];
			
			$config = $this->getClass($class_id);
			
			$columns = $config->xpath('parameter');
			$columns = $columns[0];
			$foo = new ccdsXMLparser($columns);
			$columns = $foo->getArray();
			$tmp_param_arrays[$class_id] = $this->parse2array($columns);
			return $tmp_param_arrays[$class_id];
		}


/* ------------------------------------------------------------------------------------------------------------------------------ */

    static function &getInstance( )
    {
        static $obj;

        if ( !$obj )
        {
            $config=ccdsConfig::getInstance();
            $obj=new ccdsObjectConfig();
            $obj->configure( $config->get( 'DOC_ROOT' ) . $config->get( 'WEB_ROOT' ) . "ccds_extensions/" );
        }
        return $obj;
    }
}
?>